When the GenericData is returned from the server by the component (e.g. from read operations or by subscriptions), its DataType Property property is always filled in (i.e. not null).
When the GenericData is passed in to the component for transfer to the server (e.g. to a write operation), filling in the DataType Property optional. When not null, the component checks the value in GenericData against the data type given by this property, and issues an error if the value does not conform to the type. It is allowed for this property to be not null at some higher level, and be null at lower level, such as for an individual field of a structure.
When the data type is given and passed to the component, it is only used for the checks described above. It does not influence the actual encoding of the values, and the encoding process may further reject some input values as not conforming to the encoding.
Validation rules
Generic data can only be validated if it has a non-null DataType associated with it. When the DataType is null, the validation always succeeds.
For all kinds of generic data, the associated data type must be complete and terminable for the validation to succeed. For discussions of these terms, see Working with data types.
Besides these common rules, specific rules apply to different kinds of generic data:
- For enumeration data, if the size in bits (SizeInBits Property) is given in the data type (is non-negative), the actual value must not exceed the size that fits into the number of bits given by the data type.
- For opaque data, if the size in bits (SizeInBits Property in the OpaqueDataType Class) is given in the data type (is non-negative), the actual size in bits of the opaque value must be precisely equal to the size given by the data type.
- Primitive data is always valid.
- For sequence data, all elements must be non-null and also valid. In addition, if a length is given in the data type (Length Property), the actual count of elements in the sequence must be precisely equal to the length given by the data type.
- For structured data, all field values must be non-null and also valid. In addition, all mandatory (non-optional), non-length fields that are defined by the data type must also be present in the actual field data (FieldData Property).
- For union data, if the FieldName Property (current field name) is empty, the current union value (the FieldValue Property) must be null. If the FieldName Property (current field name) is not empty, it must correspond to one of the data fields in the DataFields Property. This data field must not be a length field or switch field and must not be optional, and its data value must be null.
Invoking the validation
You can also invoke the validation of generic data from your code, e.g. to prevent errors upon writing the data. To do so, you can do one of the following:
- Inspect the IsValid Property of the generic data. The return value (a boolean) indicates whether the current data is valid.
- Call the TryValidate Method on the generic data. This method attempts to validate the current data. It returns a non-null Exception if the validations fails; it returns a null reference otherwise.
- Call the Validate Method on the generic data. This method validates the current data. It throws an exception when the validation fails.
See Also